package stores;

import models.Odontologo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

public class Dao_Odontologos {
	Dao_connection unicaConeccion=Dao_connection.getInstancia();
	
	public boolean insertarOdontologo(Odontologo od, boolean validar) {
		try {
		
			Connection connection = unicaConeccion.getConnection();
			String query = "INSERT INTO Odontologo (#Mat, nombre, apellido) VALUES (?,?,?)";
			PreparedStatement stmt = connection.prepareStatement(query);
			stmt.setString(1, od.getMatricula());
			stmt.setString(2, od.getNombre());
			stmt.setString(3, od.getApellido());
			stmt.execute();
			connection.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
			validar = false;
		}catch (Exception e) {
			e.printStackTrace();
			validar = false;
		}
		
		return validar;
		
	}

	public Vector<Odontologo> getOdontologos() {
		Vector<Odontologo> odontologos=new Vector <Odontologo>();
		
		Connection connection = unicaConeccion.getConnection();
		try{
			Statement stmt = connection.createStatement();
			String query1 = "SELECT * FROM Odontologo";
			ResultSet rs = stmt.executeQuery(query1);
			while(rs.next()){
				String matricula = rs.getString("#Mat");
				String nombre = rs.getString("Nombre");
				String apellido = rs.getString("Apellido");
				System.out.println("1.: "+ matricula + " " + nombre + " " + apellido);
				Odontologo nuevo= new Odontologo(matricula, nombre,apellido);
				odontologos.add(nuevo);
			}
			connection.close();
		}catch (SQLException e) {
			e.printStackTrace();
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		return odontologos;
	}

	public Odontologo getOdontologoPorMatricula(String matricula) {
		Odontologo buscado=null;
		Connection connection = unicaConeccion.getConnection();
		try{
			Statement stmt = connection.createStatement();
			String query1 = "SELECT * FROM Odontologo where #Mat= "+"'"+matricula+"'";
			System.out.println();
			ResultSet rs = stmt.executeQuery(query1);
			while(rs.next()){
				String mat = rs.getString("#Mat");
				String nombre = rs.getString("Nombre");
				String apellido = rs.getString("Apellido");
				System.out.println("1.: "+ matricula + " " + nombre + " " + apellido);
				buscado= new Odontologo(matricula, nombre,apellido);
			}
			connection.close();
		}catch (SQLException e) {
			e.printStackTrace();
		}catch (Exception e) {
			e.printStackTrace();
		}
		return buscado;
	}

}
